/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.utility;

import org.openaecfm.model.utility.TableRow;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.measure.ValueIO;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.TypedParameter;

public final class TableRowIO extends AbstractEntityInstanceInputImpl<TableRow>
		implements IEntityInstanceOutput<TableRow> {
	private ValueIO valueIO;

	public void setValueIO(ValueIO valueIO) {
		this.valueIO = valueIO;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(TableRow pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCTABLEROW",
				appDataManager.getName(pojo));

		// Set RowCells
		if (pojo.getRowCells() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter rowcells = new ListParameter();
			for (Object v : pojo.getRowCells()) {
				TypedParameter t = (TypedParameter) valueIO.exportSPF(v);
				rowcells.add(t);
			}
			ei.addParameter(rowcells);
		}

		// Set IsHeading
		if (pojo.getIsHeading() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			EnumerationParameter ish = new EnumerationParameter(
					pojo.getIsHeading());
			ei.addParameter(ish);
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected TableRow doCreateObject() {
		return new TableRow();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei, TableRow pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RowCells
		Parameter rowcells = sei.getParameter(0);
		if (rowcells.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) rowcells;
			for (Parameter p : l) {
				TypedParameter t = (TypedParameter) p;
				pojo.getRowCells().add(valueIO.importSPF(t));
			}
		}

		// Set IsHeading
		Parameter ish = sei.getParameter(1);
		if (ish.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) ish;
			pojo.setIsHeading(org.openaecfm.model.measure.Boolean.valueOf(e
					.toEnumString()));
		}
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei, TableRow pojo,
			IAppDataManager appDataManager) {
		return ;
	}
}
